﻿using System.Linq;
using System.Web.Mvc;
using ChiakiYu.Common.Data;
using ChiakiYu.Model.Navigations;
using ChiakiYu.Service.Authentication;
using ChiakiYu.Service.Authorization;
using ChiakiYu.Service.Navigations;
using ChiakiYu.Service.Users;
using ChiakiYu.Web.Areas.Admin.Controllers.Filters;
using ChiakiYu.Web.Controllers;
using ChiakiYu.Web.ViewModels.Account;

namespace ChiakiYu.Web.Areas.Admin.Controllers
{
    public partial class AdminController : BaseController
    {
        #region 构造函数

        public AdminController(INavigationService navigationService, IUserService userService, IAuthenticationService authenticationService, IAuthorizationService authorizationService)
        {
            _navigationService = navigationService;
            _userService = userService;
            _authenticationService = authenticationService;
            _authorizationService = authorizationService;

        }

        #endregion

        #region 私有字段

        private readonly IUserService _userService;
        private readonly IAuthenticationService _authenticationService;
        private readonly IAuthorizationService _authorizationService;

        private readonly INavigationService _navigationService;

        #endregion

        #region 主页&页头

        /// <summary>
        ///     后台主页
        /// </summary>
        /// <returns></returns>
        [ManageAuthorize(RequireSystemAdministrator = true)]
        public virtual ActionResult Home()
        {
            return View();
        }

        /// <summary>
        ///     页头
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public virtual ActionResult _Navigation(string activeMenu = "")
        {
            var list = _navigationService.GetNavigations(PresentArea.Admin);
            var permissions = _authorizationService.GetUserPermissions(UserContext.CurrentUser.Id);
            if (UserContext.CurrentUser.UserName == "admin")
                permissions = _permissionsOfAssembly;
            ViewData["Active"] = activeMenu;
            ViewData["Permissions"] = permissions;
            return PartialView(list);
        }

        #endregion

        #region 登录&注销

        /// <summary>
        ///     登录的页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual ActionResult ManageLogin()
        {
            return View();
        }

        /// <summary>
        ///     后台登录验证
        /// </summary>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public virtual ActionResult ManageLogin(LoginViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var password = UserPasswordHelper.MD5(model.PassWord);
            var user = _userService.GetAll().FirstOrDefault(n => (n.UserName == model.UserName || n.Email == model.UserName || n.Mobile == model.UserName) && n.Password == password);

            if (user != null)
            {
                _authenticationService.SignOut();
                _authenticationService.SignIn(user, model.RememberMe);
                if (string.IsNullOrWhiteSpace(model.ReturnUrl))
                {
                    return RedirectToAction(MVC.Admin.Admin.Home());
                }
                return Redirect(model.ReturnUrl);
            }
            TempData["StatusMessageData"] = "帐号或密码错误";
            return View(model);
        }
        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public virtual ActionResult Logout()
        {
            _authenticationService.SignOut();
            return RedirectToAction(MVC.Admin.Admin.ManageLogin());
        }

        #endregion
    }
}